package cn.edu.njupt.post.controller;

import cn.edu.njupt.post.model.*;
import cn.edu.njupt.post.service.*;
import cn.edu.njupt.post.util.JedisAdapter;
import cn.edu.njupt.post.util.RedisKeyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
public class FeedController {
    private static final Logger logger = LoggerFactory.getLogger(FeedController.class);

    @Autowired
    FeedService feedService;

    @Autowired
    FollowService followService;

    @Autowired
    HostHolder hostHolder;

    @Autowired
    JedisAdapter jedisAdapter;

    @Autowired
    PostService postService;

    @Autowired
    UserService userService;

    @Autowired
    LikeService likeService;

//    @RequestMapping(path = {"/pushfeeds"}, method = {RequestMethod.GET, RequestMethod.POST})
//    private String getPushFeeds(Model model) {
//        int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
//        List<String> feedIds = jedisAdapter.lrange(RedisKeyUtil.getTimelineKey(localUserId), 0, 10);
//        List<Feed> feeds = new ArrayList<Feed>();
//        for (String feedId : feedIds) {
//            Feed feed = feedService.getById(Integer.parseInt(feedId));
//            if (feed != null) {
//                feeds.add(feed);
//            }
//        }
//        model.addAttribute("feeds", feeds);
//        return "feeds";
//    }

    @RequestMapping(path = {"/pullfeeds"}, method = {RequestMethod.GET, RequestMethod.POST})
    private String getPullFeeds(Model model) {
        int localUserId = hostHolder.getUser() != null ? hostHolder.getUser().getId() : 0;
        List<Integer> followees = new ArrayList<>();
        if (localUserId != 0) {
            // 关注的人
            followees = followService.getFollowees(localUserId, EntityType.ENTITY_USER, Integer.MAX_VALUE);
            System.out.println(followees);
        }
        List<Feed> feeds = feedService.getUserFeeds(Integer.MAX_VALUE, followees, 10);
        model.addAttribute("feeds", feeds);
        model.addAttribute("posts", getPosts(followees, 10));
        return "feeds";
    }

    private List<ViewObject> getPosts(List<Integer> userIds, int count) {
        //System.out.println(postService.getLatestPostsByUser(userId, offset, limit));
        List<Post> postList;
        //if (userId == -1) postList = postService.getLatestPosts(offset, limit);
        postList = postService.getLatestPostsByUsers(userIds, count);
        List<ViewObject> vos = new ArrayList<>();
        for (Post post : postList) {
            ViewObject vo = new ViewObject();
            vo.set("post", post);
            vo.set("user", userService.getUser(post.getUserId()));
            vo.set("likeCount", likeService.getLikeCount(EntityType.ENTITY_POST, post.getId()));
            vos.add(vo);
        }
        return vos;
    }
}

